//
//  WBStatusListViewModel.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import YYModel
import SDWebImage
import SVProgressHUD

class WBStatusListViewModel: NSObject {

    
    lazy var array:[WBHomeViewModel] = [WBHomeViewModel]()
    
    func loadData(isPulling: Bool,successBlock:@escaping (_ count:Int)->()){
        
        
        guard let access_token = WBUserAccount().model?.access_token else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let max_id = isPulling ? (array.last?.status?.id ?? 0)-1 : 0
        
        
        let since_id = isPulling ? (0) : (array.first?.status?.id ?? 0)
        
        
        let parameter = [
            
            "access_token":access_token,
            "since_id":since_id,
            "max_id": max_id
            
            ] as [String : Any]
        
        WBHTTPClient.request(type: .get, urlString, parameters: parameter, success: { (json:Any) in
            
            guard let dict = json as? [String:Any] else{
                
                return
            }
            
            let statuses = dict["statuses"] as! [[String:Any]]
            
            
            if let list = NSArray.yy_modelArray(with: WBStatusModel.self, json: statuses) as? [WBStatusModel]{
            
                
                
                let group = DispatchGroup.init()
                
                var tamp : [WBHomeViewModel] = [WBHomeViewModel]()
                
                for status in list {
                
                    tamp.append(WBHomeViewModel(status: status))
                
                    
                    let pic_urls = status.pic_urls?.count != 0 ? (status.pic_urls) : (status.retweeted_status?.pic_urls)
                    
                    if let pic_urls = pic_urls{
                    
                        if pic_urls.count == 1{
                        
                            guard let urlString = pic_urls.first?.thumbnail_pic else{
                            
                                return
                            }
                        
                            group.enter()
                            
                            SDWebImageManager.shared().downloadImage(with: URL(string:urlString), options: [], progress: nil, completed: { (image, _, _, _, _) in
                                group.leave()
                            })
                            
                            
                        
                        }
                    
                    
                    
                    }
                    
                    
                }
                
                if isPulling{
                
                    self.array = self.array + tamp
                }else{
                
                    self.array = tamp + self.array
                }
                
                
               
                group.notify(queue: DispatchQueue.main, execute: { 
                    
                    
                    successBlock(tamp.count)
                })
                
                
            }
            
            
            
        }) { (error:Error) in
            
            print(error)
        }
        
        
        
    }

    
    
    
}
